package com.application.lucky.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.application.lucky.R;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;

public class WebViewActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private WebView mWebView;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initListener();
    }

    private void initListener() {
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finishAnim();
                }
            }

            @Override
            public void rightClick() {

            }
        });
    }

    @Override
    public void beforeInit() {
        url = getIntent().getStringExtra("url");

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_web_view;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mWebView = findViewById(R.id.web_view);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                showloadDialog();
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                hideLoadDialog();
                mTitleBar.setTitle(view.getTitle());

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;

            }
        });
        mWebView.loadUrl(url);
        WebSettings settings = mWebView.getSettings();
        if (settings != null) {
            settings.setJavaScriptEnabled(true);
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setDomStorageEnabled(true);
            settings.setAllowFileAccess(true);
            settings.setAllowFileAccessFromFileURLs(true);
            settings.setAllowUniversalAccessFromFileURLs(true);
        }
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {

                super.onReceivedTitle(view, title);
                mTitleBar.setTitle(title);
            }
        });

    }

    public static void jump(Context context, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finishAnim();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mWebView.loadUrl("about:blank");
        mWebView.stopLoading();
        mWebView.clearHistory();
        mWebView.clearSslPreferences();
        mWebView.removeAllViews();
        mWebView.destroy();
        mWebView = null;
    }

}
